package com.zzty.zzlp.controller;


import com.zzty.zzlp.entity.ClassGroup;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.ClassGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/Cgroup")
public class ClassGroupController {
    @Autowired
    private ClassGroupService GroupService;

    @RequestMapping("/group")
    @ResponseBody

    public List showAll(HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute("User");
        String clazzId=String.valueOf(user.getClazzId());

        System.out.println("===================================================================================");
        //String clazzId="4";
        List<User> all=this.GroupService.selectall(clazzId);
        model.addAttribute("all", all);
        int num=all.size();
        double test=(double)num/(double)2;
        if(test>num/2){
            num=num/2+1;

        }else{
            num=num/2;


        }
        model.addAttribute("Num", num);

        return all;

    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ResponseBody
    public void insert(@RequestParam(value = "group") String group, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("User");
        Integer clazzId=user.getClazzId();

        System.out.println(111111);
        //Integer clazzId=4;
        this.GroupService.delete(clazzId);
//        String group=request.getParameter("gruop");
        ClassGroup g=new ClassGroup(clazzId,group);
        this.GroupService.insertGroup(g);


    }


}
